var rightShare={
    init:function(){
        var self=this;
        self.shadeDiv = $("#shade-div");
        self.bindEvents();
        self.personnelSelection();
        $.checkAll("inputItem1");
        
    },
    bindEvents:function(){
        var self=this;
//        全选操作设置
        
//        添加用户
        $("#addUsers").click(function(){
        	art.dialog.open(ctx+"/privilege/user_privilege!select.xhtml?syspower.id="+$("#syspowerid").val()+"&a="+Math.random(), {title: '添加用户' ,width: 800, height: 550,
        		close: function(){
        			common.refreshPage();
        	    }
        	});
        	//reValue = window.showModalDialog(ctx+"/privilege/user_privilege!select.xhtml?syspower.id="+$("#syspowerid").val()+"&a="+Math.random(),"","dialogWidth=800px;dialogHeight=550px");
        	//common.refreshPage();
        	/*
        	var treeSetting = {
        		    check:{enable: false,chkStyle: "checkbox",chkboxType: { "Y": "s", "N": "ps"}},
        		    data:{simpleData: {enable: true}}
        	};
        	//加载部门数据
        	rightShare.ajaxPOST(ctx+"/privilege/user_privilege!getDeptUserTree.xhtml","",function(data){
        		$.fn.zTree.init($("#treeDemo"), treeSetting, data);
        		var treeDemo=$.fn.zTree.getZTreeObj("treeDemo");
        		rightShare.ajaxPOST(ctx+"/privilege/user_privilege!getUserIds.xhtml?syspower.id="+$("#syspowerid").val(),"",function(data){
            		for(var i=0;i<data.length;i++){
                		if(data[i].id!=""){
                			var node = treeDemo.getNodeByParam("id",data[i].id);
                			treeDemo.checkNode(node, true, true);
                		}
                	}
            	});
        	});
        	
            self.shadeDiv.show();
            $("#addUser").setPosition().show();
            */
        })
        /*
        $("#addUserButton").click(function(){
        	var deptChecked=$.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes();
        	var users="";
        	for(var i=0;i<deptChecked.length;i++){
        		if(rightShare.contains(deptChecked[i].id,"u",false)){
	        		if(i==0){
	        			users+=deptChecked[i].id;
	        		}else{
	        			users+=","+deptChecked[i].id;
	        		}
        		}
        	}
        	rightShare.ajaxPOST(ctx+"/privilege/user_privilege!addUser.xhtml?syspower.id="+$("#syspowerid").val(),"users="+users,function(data){
        		common.refreshPage();
        	});
        });
        */
        
        $("#searchDep").textClean();
        //        搜索
        $("#searchName input").textClean()
        $("#searchName input").focus(function(){
            $(this).siblings("span").show();
        })
        $("#searchName input").blur(function(){
            if($.trim($(this).val()) == ""){
                $(this).siblings("span").hide();
            };

        })
        $(".removeSearch").click(function(){
            $(this).siblings("input").val("")
        })
        $('.searchName').keydown(function(e){
            if(e.keyCode==13){
                $(this).click(); //处理事件
            }
        });
//        删除权限用户
        $(".delete").click(function(){
        	$("#sureDelId").val($(this).attr("value"));
            self.shadeDiv.show();
            $("#warning").setPosition().show().find("p.center").html("确定要删除权限用户吗？");
        })
        $("#sureDelButton").click(function(){
        	rightShare.ajaxPOST(ctx+"/privilege/user_privilege!deleteUser.xhtml?syspower.id="+$("#syspowerid").val(),"users="+$("#sureDelId").val(),function(data){
        		common.refreshPage();
        	});
        });
        $("#delete").click(function(){
            self.shadeDiv.show();
            var x=document.getElementsByName("checkbox1");
        	var u=new Array();;
        	var j=0;
        	for(var i=0;i<x.length;i++){
        		if ( x[i].checked ){
        			u[j]=x[i];
        			j++;
        		}
        	}
        	if(j==0){
        		alert("请选择用户");
        		return ;
        	}
        	var users="";
        	for (var i=0;i<u.length;i++)
        	{
        		if(i==0){
        			users+=u[i].value;
        		}else{
        			users+=","+u[i].value;
        		}
        	}
            $("#sureDelId").val(users);
            $("#warning").setPosition().show().find("p.center").html("确定要删除吗？");
        });
        $("#addMember").click(function(){
    		rightShare.treeNodeId="";
    		$("#table_left").html("");
    		$("#table_right").html("");
       	 	self.shadeDiv.show();
            $('#personnelSelection').setPosition().show();
	       	var treeSetting = {
	       		    data:{simpleData: {enable: true}},
	       			async: {
	       				enable: true,
	       				url:ctx+"/privilege/user_privilege!getUserTT.xhtml",
	       				autoParam:["id"]
	       			},callback: {
	    				onClick:self.treenodeClick
	    			}
	       	};
	       	//加载部门数据
	       	rightShare.ajaxPOST(ctx+"/privilege/user_privilege!getDeptUserTree.xhtml","",function(data){
	       		$.fn.zTree.init($("#treeDemo"), treeSetting, data);
	       		var treeDemo=$.fn.zTree.getZTreeObj("treeDemo");
	       	});
       });
    },contains:function(string,substr,isIgnoreCase){
	    if(isIgnoreCase){
		    string=string.toLowerCase();
		    substr=substr.toLowerCase();
	    }
	    var startChar=substr.substring(0,1);
	    var strLen=substr.length;
	    for(var j=0;j<string.length-strLen+1;j++){
		    if(string.charAt(j)==startChar){//如果匹配起始字符,开始查找
			    if(string.substring(j,j+strLen)==substr) {//如果从j开始的字符与str匹配，那ok
			    	return true;
			    }  
		    }
	    }
	    return false;
    },treenodeClick:function(event, treeId, treeNode, clickFlag){
    	var self=this;
    	var data=$("#personnelSelection_text").val();
  	    var postData={
              "data":data
        };
  	  rightShare.treeNodeId=treeNode.id;
    	//加载部门数据
  	  rightShare.loadLeftData(treeNode.id,postData);
    	
    }, personnelSelection:function(){
    	var self=this;
//      阻止复选框冒泡事件
      $(".selectionBot>div table tr input").live("dblclick",function(event){
          event.stopPropagation()
      })

      //        单击成员选中
      $(".selectionBot>div table tr").live("click",function () {
          var $this=$(this);
          $this.addClass("cur").siblings("tr").removeClass("cur")
      });
//      双击移动成员
      $(".selectionBot>div table tr").live("dblclick",function () {
              //双击事件的执行代码
          $(this).removeClass("cur")
          $(this).parents("div.mobile").siblings("div.mobile").find("table").append($(this));
      })
      
//      右移单个成员
      $("button.singleRight").live("click",function(){
          	var tr=$(".selectionLeft table tr.cur");
          	$(".selectionRight table").append(tr);          	
            tr.removeClass("cur");
      });
//      左移单个成员
      $("button.singleLeft").live("click",function(){
          	var tr=$(".selectionRight table tr.cur");
          	$(".selectionLeft table").append(tr);
            tr.removeClass("cur");
      });
//      右移全部成员
      $("button.allRight").live("click",function(){
          var trAll=$(".selectionLeft table tr");
          $(".selectionRight table").append(trAll);
      });
//      左移全部成员
      $("button.allLeft").live("click",function(){
          var trAll=$(".selectionRight table tr");
          $(".selectionLeft table").append(trAll);
      });
//      鼠标滑过姓名显示账号
      $(".selectionBot>div table tr div.memberName").live('hover',function(event){
          if(event.type=='mouseenter'){
              $(this).find("span").show();
          }else{
              $(this).find("span").hide();
          }
      });
      $("#pushData").click(function(){
    	  var userid=new Array() ;
    	  var trAll=$(".selectionRight table tr");
          trAll.each(function(){
        	  userid.push($(this).attr("id"));
          });
         var postData={
        	"data":userid.join(",")
         };
        //加载部门数据
       	rightShare.ajaxPOST(ctx+"/privilege/user_privilege!saveUser.xhtml?syspower.id="+$("#syspowerid").val(),postData,function(data){
       		common.refreshPage();
       	});
      });
    	
      $("#personnelSelection_search").click(function(){
    	  var data=$("#personnelSelection_text").val();
    	  var postData={
                  "data":data
          };
    	  self.loadLeftData(rightShare.treeNodeId,postData);
      });
      $("#personnelSelection_clear").click(function(){
    	  if(rightShare.treeNodeId!=""){
    		  $("#personnelSelection_text").val("");
        	  var postData={
                      "data":""
              };
        	  self.loadLeftData(rightShare.treeNodeId,postData);
    	  }
      });
    },loadLeftData:function(treeId,postData,pageNumber){
    	if(pageNumber==null){
    		pageNumber=1;
    	}
    	
    	//加载部门数据
     	rightShare.ajaxPOST(ctx+"/privilege/user_privilege!getUserListByDept.xhtml?pager.pageNumber="+pageNumber+"&id="+treeId+"&syspower.id="+$("#syspowerid").val(),postData,function(re){
     		data=re.list;
     		$("#table_left").html("");
     		var htm_left="";
     		for(var i=0;i<data.length;i++){
 				htm_left+="<tr id=\""+data[i].id+"\">" +
			"<td style=\"width:33%\"><div class=\"memberName\">"+data[i].name+"<span class=\"memberNum hide\"><em>"+data[i].name+"</em></span></div></td>" +
			"<td style=\"width:33%\">"+data[i].deptName+"</td>" +
			"<td style=\"width:30%\">"+data[i].local+"</td>" +
			"</tr>";
     		}
     		$("#table_left").html(htm_left);
     		$(".paginationLeft").unbind();
     		$('.paginationLeft').jqPagination({
                link_string : ''+re.pageNumber,
                current_page: re.pageNumber, //设置当前页 默认为1
                max_page : re.pageCount, //设置最大页 默认为1
                page_string : '当前第'+re.pageNumber+'页,共'+re.pageCount+'页',
                paged : function(page) {
                	rightShare.loadLeftData(treeId,postData,page);
                }
            });
     	});
    },
    ajaxGET:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"GET", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    },
    ajaxPOST:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"POST", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    }
}
$(function(){
    rightShare.init();
})